package com.myCode.DAO.DaoImpl;

import com.myCode.DAO.ProductDao;
import com.myCode.Util.MybatisUtil;
import com.myCode.entity.Product;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProductDaoImpl implements ProductDao {
    private static SqlSession session = MybatisUtil.getSession();

    @Override
    public void add(Product product) {
        int insert = session.insert("com.Mapper.ProductMapper.add", product);
        if (insert == 0) {
            throw new RuntimeException("数据未能写入");
        }
        session.commit();
//        session.close();
    }

    @Override
    public void delById(Integer id) {
        int delById = session.delete("com.Mapper.ProductMapper.delById", id);
        if (delById == 0) {
            throw new RuntimeException("数据未能写入");
        }
        session.commit();
//        session.close();
    }

    @Override
    public void edit(Product product) {
        int edit = session.update("com.Mapper.ProductMapper.edit", product);
        if (edit == 0) {
            throw new RuntimeException("数据未能写入");
        }
        session.commit();
//        session.close();
    }

    @Override
    public List<Product> selAll() {
//        session.close();
        return session.selectList("com.Mapper.ProductMapper.selAll");
    }

    @Override
    public Product selOneById(Integer id) {
        return session.selectOne("com.Mapper.ProductMapper.selOneById",id);
    }
}
